<template>
  <div>
    <box t1="商品管理" t2="商品分类">
      <!-- 添加分类 -->
      <el-row>
        <el-col>
          <el-button type="primary" @click="showCateDialogVisible">添加分类</el-button>
        </el-col>
      </el-row>
      <!-- 表格区域 -->
      <br>
      <tree-table
        :data="catelist"
        :columns="columns"
        :show-row-hover="false"
        :selection-type="false"
        :expand-type="false"
        show-index
        index-text="#"
      >
        <!-- 是否有效 -->
        <template slot="isOk" slot-scope="scope">
          <i class="el-icon-success" style="color:lightgreen" v-if="scope.row.cat_deleted===false"></i>
          <i class="el-icon-error" style="color:red" v-else></i>
        </template>
        <!-- 排序 -->
        <template slot="order" slot-scope="scope">
          <el-tag type="" size="mini" v-if="scope.row.cat_level===0">一级</el-tag>
          <el-tag type="success" size="mini" v-else-if="scope.row.cat_level===1">二级</el-tag>
          <el-tag type="warning" size="mini" v-else>三级</el-tag>
        </template>
        <!-- 操作 -->
        <template slot="opt" slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" size="mini">编辑</el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini">删除</el-button>
        </template>
      </tree-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 3, 5, 10]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </box>
    <!-- 添加分类弹框 -->
    <el-dialog title="商品分类" :visible.sync="addCateDialogVisible" width="50%" @close="resetCateForm">
      <el-form :model="addCateForm" :rules="addCateRules" ref="addCateRefs" label-width="90px">
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="addCateForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父类名称">
          <el-cascader
            v-model="selectionKeys"
            :options="parentList"
            :props="addCateProps"
            @change="parentCateChange"
            clearable
          ></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addCateDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      catelist: [],
      queryInfo: {
        type: 3,
        pagesize: 5,
        pagenum: 1
      },
      total: 0,
      columns: [
        {
          label: "分类名称",
          prop: "cat_name"
        },
        {
          label: "是否有效",
          type: "template",
          template: "isOk"
        },
        {
          label: "排序",
          type: "template",
          template: "order"
        },
        {
          label: "操作",
          type: "template",
          template: "opt"
        }
      ],
      addCateDialogVisible: false,
      addCateForm: {
        cat_name: "",
        // 父级分类的id
        cat_pid: 0,
        cat_level: 0
      },
      addCateRules: {
        cat_name: [
          {
            required: true,
            message: "请输入商品分类",
            trigger: "blur"
          }
        ]
      },
      // 一二级分类数据
      parentList: [],
      // 选中的值是一个数组
      selectionKeys: [],
      addCateProps: {
        // 级联选择器的配置
        value: "cat_id",
        label: "cat_name",
        children: "children",
        // 设置鼠标滑过触发
        expandTrigger: "hover",
        // 可选父节点
        checkStrictly: true
      }
    };
  },
  created() {
    this.getCategoriesList();
  },
  methods: {
    async getCategoriesList() {
      const res = await this.$http.get("categories", {
        params: this.queryInfo
      });
      if (res.meta.status != 200)
        return this.$message.error("获取商品分类失败");
      this.catelist = res.data.result;

      this.total = res.data.total;
    },
    handleSizeChange(val) {
      this.queryInfo.pagesize = val;
      this.getCategoriesList();
    },
    handleCurrentChange(val) {
      this.queryInfo.pagenum = val;
      this.getCategoriesList();
    },
    showCateDialogVisible() {
      this.addCateDialogVisible = true;
      this.getParentCate();
    },
    async getParentCate() {
      const res = await this.$http.get("categories", {
        params: {
          type: 2
        }
      });
      if (res.meta.status != 200)
        return this.$message.error("获取父分类数据失败");
      this.parentList = res.data;
    },
    parentCateChange() {
      // 判断用户是否创建的是一级分类
      if (this.selectionKeys.length > 0) {
        this.addCateForm.cat_pid = this.selectionKeys[
          this.selectionKeys.length - 1
        ];
        this.addCateForm.cat_level = this.selectionKeys.length;

        return;
      } else {
        this.addCateForm.cat_pid = 0;
        this.addCateForm.cat_level = 0;
      }
    },
    addCate() {
      this.$refs.addCateRefs.validate(async valid => {
        if (!valid) return;
        const res = await this.$http.post("categories", this.addCateForm);
        if (res.meta.status != 201)
          return this.$message.error("添加商品分类失败");
        this.addCateDialogVisible = false;
        this.getCategoriesList();
      });
    },
    resetCateForm() {
      this.$refs.addCateRefs.resetFields();

      this.selectionKeys = [];
      this.addCateForm.cat_pid = 0;
      this.addCateForm.cat_level = 0;
    }
  }
};
</script>

<style lang="less" scoped>
.el-cascader {
  width: 100%;
}
</style>